//
//  MyReportListViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/6/2.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "MyReportListViewController.h"
#import "SimpleSeg.h"
#import "ReportListCell.h"
#import "CustomReportViewController.h"
#import "ReportDetailViewController.h"
#import "AnalysisViewController.h"
#import "ReportViewModel.h"
@interface MyReportListViewController ()<SimpleSegDelegate,UITableViewDelegate,UITableViewDataSource>{
    
}
@property(nonatomic,strong)SimpleSeg *segment;
@property(nonatomic,weak)IBOutlet UITableView *mainTableView;
@property(nonatomic,weak)IBOutlet UIButton *fenxiBtn;
@property(nonatomic,assign)BOOL isAlreadyExistReport;
@property(nonatomic,strong) ReportViewModel *reportViewModel;
@property(nonatomic,assign)NSInteger selectIndex;
@property(nonatomic,strong)NSMutableArray *reportList;
@end

@implementation MyReportListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationController.navigationBar addSubview:self.segment];
    self.fenxiBtn.layer.cornerRadius = 20;
    self.fenxiBtn.hidden = YES;
    self.isAlreadyExistReport = YES;
    [self configTableView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self requestData];
}
-(void)requestData{
    [self.reportViewModel fetchReportList:self.selectIndex cmp:^(NSMutableArray * _Nonnull reportList) {
        [self.reportList removeAllObjects];
        [self.reportList addObjectsFromArray:reportList];
        [self.mainTableView reloadData];
    }];
}
-(void)configTableView{
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [UIView new];
    [self.mainTableView registerNib:[UINib nibWithNibName:@"ReportListCell" bundle:nil] forCellReuseIdentifier:@"ReportListCell"];
    self.mainTableView.showsVerticalScrollIndicator = NO;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainTableView.backgroundColor = [UIColor clearColor];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.reportList count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 76;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReportListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReportListCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.contentView.backgroundColor = [UIColor clearColor];
    if (indexPath.row==0) {
        [cell.bgView setImage:[[UIImage imageNamed:@"topBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"topBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"topBg"].size.height/2]];
    }else if (indexPath.row<[self.reportList count]-1){
        [cell.bgView setImage:[[UIImage imageNamed:@"middleBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"middleBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"middleBg"].size.height/2]];
    }else{
        [cell.bgView setImage:[[UIImage imageNamed:@"bottomBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"bottomBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"bottomBg"].size.height/2]];
    }
    [cell setReportValue:[self.reportList objectAtIndex:indexPath.row]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomReportViewController *customReport = [[CustomReportViewController alloc]init];
    customReport.reportModel = [self.reportList objectAtIndex:indexPath.row];
    [self.rt_navigationController pushViewController:customReport animated:YES complete:nil];
}
#pragma mark - instance
-(SimpleSeg*)segment{
    if (!_segment) {
        _segment = [[SimpleSeg alloc]initWithFrame:CGRectMake(100, 0, DEVICE_WIDTH-200, 44) delegate:self];
    }
    return _segment;
}
-(ReportViewModel*)reportViewModel{
    if (!_reportViewModel) {
        _reportViewModel = [[ReportViewModel alloc]init];
    }
    return _reportViewModel;
}
-(NSMutableArray*)reportList{
    if (!_reportList) {
        _reportList = [[NSMutableArray alloc]init];
    }
    return _reportList;
}
#pragma mark - segDelegate
-(NSArray*)titlesForSeg{
    return @[@"已有报告",@"定制报告"];
}
-(void)simpleSeg:(SimpleSeg *)simpleseg didselectIndex:(NSInteger)index{
    self.selectIndex = index;
    if (index==0) {
        self.isAlreadyExistReport = YES;
        self.fenxiBtn.hidden = YES;
    }else{
        self.isAlreadyExistReport = NO;
        self.fenxiBtn.hidden = NO;
    }
    [self requestData];
}
-(IBAction)fenxiBtnPressed:(id)sender{
    AnalysisViewController *fenxi = [[AnalysisViewController alloc]init];
    [self.rt_navigationController pushViewController:fenxi animated:YES complete:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
